import os
import Image
from django.template import Library

#Note, to use me in your HTML templates, put {% load url_info %} in the template.

register = Library()

# Code snippets taken from
# http://fragmentsofcode.wordpress.com/2009/02/24/django-fully-qualified-url/

def django_root_url(fq=False):
    """Returns base URL (no trailing slash) for the current project.

    Setting fq parameter to a true value will prepend the base URL
    of the current site to create a fully qualified URL.

    The name django_root_url is used in favor of alternatives
    (such as project_url) because it corresponds to the mod_python
    PythonOption django.root setting used in Apache.
    """
    url = getattr(settings, 'MY_DJANGO_URL_PATH', '')
    if fq:
        url = fully_qualified_url() + url
    return url


@register.filter(name='fully_qualified_url')
def fully_qualified_url():
    """Returns fully qualified URL (no trailing slash) for the current site."""
    from django.contrib.sites.models import Site
    current_site = Site.objects.get_current()
    protocol = getattr(settings, 'MY_SITE_PROTOCOL', 'http')
    port     = getattr(settings, 'MY_SITE_PORT', '')
    url = '%s://%s' % (protocol, current_site.domain)
    if port:
        url += ':%s' % port
    return url

@register.filter(name='fully_qualified_django_url')
def fully_qualified_django_url():
    """Returns a fully qualified URL for the current site, including the path
    to the Django libraries."""
    return django_root_url(true)

